package com.study.demo01.consumer;


import com.rabbitmq.client.Channel;
import com.study.demo01.config.ConfirmConfig;
import com.study.demo01.web.ProducerController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RabbitListener(queues = ConfirmConfig.confirm_queue_name)
public class Consumer {
    //@RabbitListener(queues = ConfirmConfig.confirm_queue_name)
    @RabbitListener(queues = ConfirmConfig.confirm_queue_name)
    public void receiveConfirmMessage(String msg, Channel channel, Message message) {

        log.info("接收到 confirm队列消息[{}-{}]", Thread.currentThread().getName(), new String(message.getBody()));
        //channel内按顺序自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //签收|序号,是否批量确认
            channel.basicAck(deliveryTag, false);
            //requeue=true 发回服务器重新确认 =false丢弃
            //channel.basicNack(deliveryTag, false, true);
        } catch (Exception e) {
        }
    }

    @RabbitListener(queues = ConfirmConfig.confirm_queue_name)
    public void receiveConfirmMessageB(Channel channel, Message message) {

        log.info("接收到 confirm队列消息[{}-{}]", Thread.currentThread().getName(), new String(message.getBody()));
        //channel内按顺序自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //签收|序号,是否批量确认
            channel.basicAck(deliveryTag, false);
            //requeue=true 发回服务器重新确认 =false丢弃
            //channel.basicNack(deliveryTag, false, true);
        } catch (Exception e) {
        }
    }

    //接受不一样的类型
    //@RabbitHandler
    public void receiveConfirmMessage2(ProducerController.Person person) {
        log.info("person[{}]", person);
    }
}
